#include <algorithm>
#include <cassert>
#include <vector>

using namespace std;

class Solution {
public:
    int maxOperations(vector<int>& nums, int k) {
        sort(nums.begin(), nums.end());

        int left = 0, right = nums.size() - 1, count = 0;
        while (left < right) {
            int sum = nums[left] + nums[right];
            if (sum < k) {
                ++left;
            } else if (sum > k) {
                --right;
            } else {
                ++left;
                --right;
                ++count;
            }
        }

        return count;
    }
};

int main() {
    Solution s;

    vector<int> nums1{1, 2, 3, 4};
    vector<int> nums2{3, 1, 3, 4, 3};

    assert(s.maxOperations(nums1, 5) == 2);
    assert(s.maxOperations(nums2, 6) == 1);

    return 0;
}